// pages/navigation/navigation.js
import QQMap from "../../utils/QQMAP"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: 40.060539,
    longitude: 116.343847,
    scale:9,
    markers:[
      {
        id:1,
        latitude: 40.060539,
        longitude: 116.343847,
        width:24,
        height:30
      },
      {
        id: 2,
        latitude: 40.225857999999995,
        longitude: 116.23246699999999,
        width: 40,
        height: 40,
      },
    ],
    polyline:[]
  },
  getPolyline(){
    QQMap.direction({
      mode:"bicycling",
      from:'40.225857999999995,116.23246699999999',
      to:'40.060539,116.343847',
      success:res=>{
        const coors = res.result.routes[0].polyline
        const points = []
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / 1000000
        }
        // 获取经纬度
        for (let i = 0; i < coors.length; i += 2) {
          points.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        // 渲染数据
        this.setData({
          polyline: [
            {points, color: '#DE563F', width: 4},
          ],
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPolyline()
  },
})